package com.boot.dog.common.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.boot.dog.common.constant.Const;
import com.boot.dog.common.exception.DogException;
import lombok.experimental.UtilityClass;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 * @Author: MaxLv
 * @Date: 2020/1/18
 */
@UtilityClass
public class FileUtil {


    /**
     * 取得临时文件夹
     *
     * @return
     */
    public String getTempFolder() {
        String tempDir = Const.TEMP_DIR;
        mkdirs(tempDir);
        return tempDir;
    }

    /**
     * 取得图片文件夹
     *
     * @return
     */
    public String getNasFolder() {
        String nasDir = Const.NAS_DIR;
        mkdirs(nasDir);
        return nasDir;
    }

    /**
     * 获取临时文件
     *
     * @param filename
     * @return
     */
    public File getTempFile(String filename) {
        return new File(getTempFolder(), filename);
    }

    /**
     * 获取文件
     *
     * @param filename
     * @return
     */
    public File getNasFile(String filename) {
        return new File(getNasFolder(), filename);
    }

    /**
     * 删除NAS中的文件用
     *
     * @param filename 文件名
     */
    public void deleteFileFromNas(String filename) {
        deleteFile(getNasFolder(), filename);
    }

    /**
     * 删除文件用
     *
     * @param path     文件路径
     * @param filename 文件名
     */
    public void deleteFile(String path, String filename) {
        File file = new File(path, filename);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 文件移动
     *
     * @param filename
     * @return
     */
    public String moveFileTempToNas(String filename) {
        return moveFile(filename, getTempFolder(), filename, getNasFolder());
    }

    /**
     * 文件移动
     *
     * @param filename
     * @return
     */
    public String moveFileTempToNasFolder(String filename, String destFolder) {
        return moveFile(filename, getTempFolder(), filename,
                FilenameUtils.concat(FileUtil.getNasFolder(), destFolder));
    }

    /**
     * 文件移动
     *
     * @param filename
     * @param fromFolder
     * @param destFolder
     * @return
     */
    public String moveFile(String filename, String fromFolder, String destFolder) {
        return moveFile(filename, fromFolder, filename, destFolder);
    }

    /**
     * 文件移动
     *
     * @param filename
     * @param fromFolder
     * @param destFileName
     * @param destFolder
     * @return
     */
    public String moveFile(String filename, String fromFolder, String destFileName, String destFolder) {
        try {
            File srcFile = new File(fromFolder, filename);
            if (!srcFile.exists()) {
                throw new DogException("文件不存在");
            }

            String dirName = new SimpleDateFormat("yyyyMM").format(new Date());
            File dateDir = new File(destFolder, dirName);
            if (!dateDir.exists()) {
                dateDir.mkdirs();
            }

            File destFile = new File(dateDir, destFileName);
            if (destFile.exists()) {
                destFile.delete();
            }

            FileUtils.moveFile(srcFile, destFile);

            return FilenameUtils.concat(dirName, destFileName);
        } catch (IOException e) {
            throw new DogException("保存文件失败");
        }
    }

    /**
     * 文件拷贝
     *
     * @param filename
     * @param fromFolder
     * @param destFileName
     * @param destFolder
     * @return
     */
    public boolean copyFile(String filename, String fromFolder, String destFileName, String destFolder) {
        File srcFile = new File(fromFolder, filename);
        if (!srcFile.exists()) {
            return false;
        }

        String dirName = new SimpleDateFormat("yyyy/MM").format(new Date());
        File dateDir = new File(destFolder, dirName);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }

        File destFile = new File(destFolder, destFileName);
        if (destFile.exists()) {
            return false;
        }

        cn.hutool.core.io.FileUtil.copyFile(srcFile, destFile);

        return true;
    }

    /**
     * 创建文件夹
     *
     * @param dir
     */
    public void mkdirs(String dir) {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 创建文件夹
     *
     * @param parent
     * @param child
     */
    public void mkdirs(String parent, String child) {
        File file = new File(parent, child);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

}
